package com.hb.gp.util.command;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import com.hb.gp.util.MsgGenUtil;

public class CmdToken {
	 static BigInteger n;
	 static BigInteger d;
	 
	 public static void main(String[] args) throws Exception{
		 /*test*/
		 String key_n="AD5E61FA6F27ABE06E31B20B990171CE7F6C8F34B59CE5887F0FA4E03A45DB518638E35F94AD65986DBCA4303C9483F836B0DA24A7DC5D5D43B975E86D5A1AE49D293CE46BFF5AF8DFE69F2523231E04FAA58C6DDE68BA2071CEC1F5DE452489E243FEFEB2D69AE427CE268D88932E0ADAAF55B2DD36461DA62251342FD6D7B1";
		 String key_d="19D241E38BE4ABBBCC8DC847E3ED0C27466143DF04E1AE6A2B0A8873011AE0747EFA401B3F48A82610CA14E63E4F9C6C3EB10E7C8127D81C08F7397179BFFEDB28B1553BE28022D2F771A58578D944575537AE4114CBA3B9C985F11307000101F041E87985D0F30A21AA3DE3637B9439D13FD4A3290875CE0DA38D2463E3B7A5";
		 String source="986810011611809036420C006F10F0000000000191452F0207010000000110F0000000000191452F0207010000004110A0000003330101060003030000000101010039B6124207FFFFFFFFFFFFFF4507FFFFFFFFFFFFFFEF1EA008810101A5038201C0A112A3104F0E325041592E5359532E4444463031C903071002";
		 //获得摘要
		 byte[] data = SHA1(source);
		 byte[] msg = t(data,key_n.length()/2);	
		 d= new BigInteger(key_d,16);
		 n= new BigInteger(key_n,16);
		 BigInteger enmsg = new BigInteger(msg);
		 BigInteger demsg=enmsg.modPow(d,n);		
		 System.out.println("解密消息："+MsgGenUtil.buf2hex(demsg.toByteArray()));   
	 }
	 /**
	  * @param key_n n
	  * @param key_d d
	  * @param source data要加密的明文
	  * @ruturn 1 error
	  * 
	  * */
	 public static String getToken(String key_n, String key_d, String source){
		 //获得摘要
		 try{
			 byte[] data = SHA1(source);
			 byte[] msg = t(data,key_n.length()/2);	
			 d= new BigInteger(key_d,16);
			 n= new BigInteger(key_n,16);
			 BigInteger enmsg = new BigInteger(msg);
			 BigInteger demsg=enmsg.modPow(d,n);		
			 return MsgGenUtil.buf2hex(demsg.toByteArray());
		 }catch(Exception e){
			 return "1";
		 }
	 }
	 private static byte[] t(byte[] data,int n){
		 byte[] DigestAlgorithmID = new byte[]{0x30,0x21,0x30,0x09,0x06,0x05,0x2b,0x0e,0x03,0x02,0x1a,0x05,0x00,0x04,0x14};
		 byte[] t = new byte[n];
//		 n = length		 
//		 length = n-3-15-20;//3:固定,15 did长度,20 data长度
		 for(int i=0;i<n;i++){
			 t[i]=(byte)0xFF;
		 }
		 t[0]=0x00;
		 t[1]=0x01;
		 t[n-36]=0x00;
		 System.arraycopy(DigestAlgorithmID, 0, t, n-35, DigestAlgorithmID.length);
		 System.arraycopy(data, 0, t, n-20, data.length);
		 System.out.println(MsgGenUtil.buf2hex(t));
		 return t;
	 }
	 
	 public static byte[] SHA1(String indata){
		 MessageDigest md = null;
		 byte[] digest =  null;
		 try{
			 md = MessageDigest.getInstance("SHA");
			 try {
				digest = md.digest(MsgGenUtil.BCD2Bytes(indata));
				System.out.println("sha1:"+MsgGenUtil.buf2hex(digest));
			} catch (Exception e) {
				e.printStackTrace();
			}
		 }catch(NoSuchAlgorithmException e){
			 e.printStackTrace();
		 }
		 return digest;
	 }
}
